package com.fm.demo.utils

import android.app.Dialog
import android.content.Context
import android.graphics.Bitmap
import android.text.SpannableString
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.fm.demo.R

/**
 * @CreateData:     2019/11/1 9:33
 * @E-Mail:         709674174@qq.com
 * @Author:         lyy
 * @Description:    dialog 工具
 */
object DialogUtils {
    private var loadingDialog: Dialog? = null

    /** 提示dialog */
    fun showPromptDialog(
        spannableString: SpannableString,
        callback: DialogCallback?,
        title: String = "温馨提示",
        context: Context = ActivityUtils.getTopActivity()
    ) {
        MaterialDialog(context)
            .show {
                title(text = title)
                message(text = spannableString)
                positiveButton(text = "确定") {
                    if (null == callback) {
                        dismiss()
                    } else {
                        callback.positiveClick()
                    }
                }
                negativeButton(text = "取消") {
                    if (null == callback) {
                        dismiss()
                    } else {
                        callback.negativeClick()
                    }
                }
            }
    }

    /** 等待dialog  */
    fun showLoadingDialog(
        title: String = "正在加载...",
        context: Context = ActivityUtils.getTopActivity()
    ): Dialog {
        loadingDialog =
            MaterialDialog(context)
                .show {
                    view.background = null
                    customView(R.layout.dialog_loading_layout)
                    getCustomView().let {
//                        it.findViewById<ContentLoadingProgressBar>(R.id.dialog_loading_clp)
//                            .indeterminateDrawable.colorFilter = BlendModeColorFilter(
//                            ContextCompat.getColor(context, R.color.colorPrimary),
//                            BlendMode.MULTIPLY
//                        )
                        it.findViewById<TextView>(R.id.dialog_loading_msg).text = title
                    }
                    cancelOnTouchOutside(false)
                    cancelable(true)
                }
        val params = loadingDialog!!.window!!.attributes
        params.width = ConvertUtils.dp2px(130f)
        params.height = ConvertUtils.dp2px(130f)
        loadingDialog!!.window!!.attributes = params
        return loadingDialog!!
    }

    fun dismissLoadingDialog() {
        loadingDialog?.dismiss()
        loadingDialog = null
    }

    /** 展示图片 */
    fun showImageDialog(context: Context, imageUrl: String, bitmap: Bitmap? = null) {
        val dialog = MaterialDialog(context)
            .show {
                customView(R.layout.dialog_image_layout)
                getCustomView().setOnClickListener {
                    dismiss()
                }
                val contentImageView =
                    getCustomView().findViewById<ImageView>(R.id.dialog_iv_content)
                Glide.with(context).load(
                    bitmap ?: imageUrl
                ).into(contentImageView)
                window?.setBackgroundDrawable(null)
            }
        dialog.view.maxHeight = ScreenUtils.getScreenHeight()
    }

    /**
     * 设置dialog 窗口属性
     * @param dialog 设置属性dialog对象
     * @param width 宽度百分比
     * @param height 高度百分比
     * @param gravity 显示位置
     */
    fun dialogSetAttr(dialog: Dialog, width: Float, height: Float, gravity: Int) {
        val params = dialog.window?.attributes
        params?.width = if (width == 0f) {
            WindowManager.LayoutParams.MATCH_PARENT
        } else {
            (ScreenUtils.getScreenWidth() * width).toInt()
        }
        params?.height = if (height == 0f) {
            WindowManager.LayoutParams.WRAP_CONTENT
        } else {
            (ScreenUtils.getScreenHeight() * height).toInt()
        }
        dialog.window?.attributes = params
        dialog.window?.setGravity(gravity)
    }

    interface DialogCallback {
        fun positiveClick(data: Any? = null) {}
        fun negativeClick() {}
        fun itemClick(index: Int, item: String) {}
    }
}